package com.wandian.ui.controls
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import com.wandian.ui.events.ButtonEvent;
	import com.wandian.ui.events.ScrollBarEvent;
	import com.wandian.ui.skins.Skin;



	public class ScrollBar extends UIComponent
	{
		private var upArrow:Button;

		private var downArrow:Button;

		private var thumb:Button
		private var isScrolling:Boolean=false;
		private var _scrollPosition:Number=0
		protected var pageSize:int=0;
		protected var minScrollPosition:int=0;
		protected var maxScrollPosition:int=0;
		protected var trackHeight:int;
		protected var thumbDragRange:Rectangle
		protected var trackY:int
		protected var skin:Skin;
		private var minThumbHeight:Number=20
		private var scrolling:Boolean=false 

		public function ScrollBar()
		{
			super(16, 150);
			this.mouseEnabled=false
			this.tabChildren=false;
		}

		override protected function createChildren():void
		{
			thumbDragRange=new Rectangle();
			
			var skinClass:Class=this.getStyle("skin") as Class
			skin=new skinClass() as Skin
			skin.styleClient=this
			this.addChild(skin);

			upArrow=new Button()
			upArrow.name="upArrow"
			upArrow.setStyleDeclaration(getStyle("upArrowStyle"))
			upArrow.initialize();
			this.addChild(upArrow);

			thumb=new Button();
			thumb.name="scrollThumb"
			thumb.setStyleDeclaration(getStyle("ScrollThumbStyle"));
			thumb.height=50;
			thumb.initialize();
			this.addChild(thumb)
			thumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

			downArrow=new Button()
			downArrow.name="downArrow"
			downArrow.setStyleDeclaration(getStyle("downArrowStyle"))
			downArrow.initialize();
			this.addChild(downArrow);


		}

		private function mouseDownHandler(e:MouseEvent):void
		{
			scrolling=true;
			thumb.addEventListener(ButtonEvent.Release, mouseUpHandler)
			manager.getStage().addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler)
			thumb.startDrag(false, thumbDragRange);
		}

		private function mouseMoveHandler(e:MouseEvent):void
		{
			_scrollPosition=(thumb.y-trackY)/(trackHeight-thumb.height)*(maxScrollPosition-minScrollPosition)+minScrollPosition
			this.dispatchEvent(new ScrollBarEvent(ScrollBarEvent.Scroll,_scrollPosition));
		}

		private function mouseUpHandler(e:ButtonEvent):void
		{
			scrolling=false;
			manager.getStage().removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler)
			thumb.removeEventListener(ButtonEvent.Release, mouseUpHandler)
			thumb.stopDrag();
		}

		override protected function layoutContents(w:int, h:int):void
		{
			skin.updateDisplay(w, h,null);
			trackY=thumb.width=upArrow.width=upArrow.height=downArrow.width=downArrow.height=w
			trackHeight=Math.max(h - w * 2, 0)
			downArrow.y=h - w
			thumbDragRange.y=trackY
			setScrollProperties(this.pageSize, this.minScrollPosition, this.maxScrollPosition)
		}

		public function setScrollProperties(pageSize:Number, minScrollPosition:Number, maxScrollPosition:Number):void
		{
			this.pageSize=pageSize;
			this.minScrollPosition=Math.max(minScrollPosition, 0)
			this.maxScrollPosition=Math.max(maxScrollPosition, this.minScrollPosition);
			//更新Thumb的位置
			scrollPosition=Math.min(this.maxScrollPosition, Math.max(this.minScrollPosition, _scrollPosition));
			if (this.maxScrollPosition > this.minScrollPosition)
			{
				thumb.enabled=upArrow.enabled=downArrow.enabled=enabled
				thumb.visible=true
				thumb.height=Math.max(Math.round(pageSize / (this.maxScrollPosition - this.minScrollPosition + pageSize) * trackHeight), minThumbHeight);
				thumbDragRange.height=trackHeight - thumb.height
			}
			else
			{
				thumb.enabled=this.upArrow.enabled=this.downArrow.enabled=false
				thumb.visible=false
			}
		}

		public function get scrollPosition():Number
		{
			return _scrollPosition;
		}

		public function set scrollPosition(v:Number):void
		{
			if(scrolling){
				return
			}
			_scrollPosition=v;
			thumb.y=Math.round((v - minScrollPosition) * (trackHeight - thumb.height) / (maxScrollPosition - minScrollPosition) + trackY);

		}
	}
}
